//
//  PhoneRechagePayInfoController.m
//  HuLaQuan
//
//  Created by hok on 3/9/16.
//  Copyright © 2016 yuwubao. All rights reserved.
//

#import "PhoneRechagePayInfoController.h"



@interface PhoneRechagePayInfoController ()



@end

@implementation PhoneRechagePayInfoController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
- (void)setupChildControllerUI
{
    self.title = @"话费充值";
    
    self.view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
}
- (void)updateUI
{
    self.showPayment.text = [NSString stringWithFormat:@"¥%@",ServerMoneyFloatToShow(self.orderInfo[@"payAmt"])] ;
    self.payName.text = self.orderInfo[@"resName"];
    
    self.userCardList = self.cardList;
    
    self.selectedPath = [NSIndexPath indexPathForRow:1 inSection:0];
    [self.payTableView selectRowAtIndexPath:self.selectedPath animated:NO scrollPosition:UITableViewScrollPositionTop];
}



#pragma mark - Button Events

/**
 *  关闭交易信息，取消交易
 *
 *  @param sender <#sender description#>
 */
- (IBAction)tapCloseButton:(id)sender {
    UIAlertController *x = [UIAlertController alertWithTitle:@"提示" andMessage:@"是否放弃充值操作" andBlock:^(UIAlertAction *action ) {
        if ([action.title isEqualToString:@"好的"]) {
            [self dismissViewControllerAnimated:YES completion:^{if ([self.delegate respondsToSelector:@selector(cancelBtnClick:)]) {
                [self.delegate cancelBtnClick:YES];
            }}];
        }
        
    }  type:@"confirmAndCancel"];
    [self presentViewController:x animated:YES completion:nil];
}

/**
 *  选择其他银行卡，更换银行卡
 */
- (void)changeCard
{
    if(self.orderInfo && self.cardList.count)
    {
        [YuNetworkStatusUtilities showProgressMessage:NETWORK_LOADING];
        [self.convenientPayConfirmEngine getPaymentInfo:self.cardList[self.selectedPath.row - 1] andOrderInfo:self.orderInfo];
        
        
    }
}
/**
 *  确认支付交易
 */
-(void)tapPayButton
{
    
    [self dismissViewControllerAnimated:YES completion:^{if ([self.delegate respondsToSelector:@selector(okBtnClick:withCard:)]) {
        [self.delegate okBtnClick:YES withCard:self.cardList[self.selectedPath.row - 1]];
    }}];
    
    
    
}
#pragma mark - CallBack

- (void)getConvenientPaymentInfoFinish:(NSNotification*) notification
{
    [YuNetworkStatusUtilities dismissHUD];
    NSDictionary *messageDic = [notification object];//获取到传递的对象
    if ([messageDic[@"isSuccess"] boolValue]) {
        
        NSLog(@"支付信息确认成功");
        
    }else{
        [self showNetWorkError:messageDic[@"message"]];
        
    }
    
}
#pragma mark - Life Cycle
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getConvenientPaymentInfoFinish:) name:@"GetConvenientPaymentInfoFinish" object:nil];
  
    
    [self updateUI];
    
    if(self.orderInfo && self.cardList.count)
    {
        [YuNetworkStatusUtilities showProgressMessage:NETWORK_LOADING];
        [self.convenientPayConfirmEngine getPaymentInfo:self.cardList[self.selectedPath.row - 1] andOrderInfo:self.orderInfo];
        
        
    }
    
}
- (void)viewDidAppear:(BOOL)animated
{
//    if(self.orderInfo && self.cardList.count)
//    {
//        [YuNetworkStatusUtilities showProgressMessage:NETWORK_LOADING];
//        [self.convenientPayConfirmEngine getPaymentInfo:self.cardList[self.selectedPath.row - 1] andOrderInfo:self.orderInfo];
//        
//        
//    }
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:YES];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"GetConvenientPaymentInfoFinish" object:nil];
    
   
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
